﻿package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import client.Configuration;
import server.ResBookInterface;
import server.ResCustomerInterface;
import server.ResDinnerInterface;
import server.ResWaiterInterface;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;

import client.Configuration;
import java.awt.Font;
import javax.swing.ImageIcon;

public class ResBookLoginGUI extends JDialog {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;

	
	
	private ResBookInterface book;
	/**
	 * Launch the application.
	 
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ResBookLoginGUI frame = new ResBookLoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	*//**
	 * Create the frame.
	 * @throws NotBoundException 
	 * @throws RemoteException 
	 * @throws MalformedURLException 
	 */
	public ResBookLoginGUI() throws MalformedURLException, RemoteException, NotBoundException {
		setTitle("\u9884\u8BA2\u4FE1\u606F\u9A8C\u8BC1");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 358, 254);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u8BF7\u8F93\u5165\u60A8\u7684\u8BA2\u5355\u53F7\uFF1A");
		label.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label.setBounds(28, 28, 175, 20);
		contentPane.add(label);
		
		textField = new JTextField();
		textField.setBounds(193, 29, 115, 23);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("\u8BF7\u8F93\u5165\u60A8\u7684\u624B\u673A\u53F7\uFF1A");
		label_1.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label_1.setBounds(28, 69, 162, 23);
		contentPane.add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(193, 72, 115, 22);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		Configuration config=new Configuration();
		String rmiAddress=config.getRmiAddress();
		ResBookInterface book = (ResBookInterface) Naming.lookup(rmiAddress+"ResBookInfo");
		
		
		JButton button = new JButton("\u786E\u5B9A");
		button.setFont(new Font("方正舒体", Font.PLAIN, 18));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Configuration config= new Configuration();
				String rmiAddress=config.getRmiAddress();
				
				try {
					ResBookInterface book= (ResBookInterface) Naming.lookup(rmiAddress+"ResBookInfo");
					ResWaiterInterface waiter=(ResWaiterInterface)Naming.lookup(rmiAddress+"ResWaiter");
					ResCustomerInterface customer= (ResCustomerInterface) Naming.lookup(rmiAddress+"ResCustomer");
					ResDinnerInterface dinner= (ResDinnerInterface) Naming.lookup(rmiAddress+"ResDinnerInfo");
					
					int i;
					String bookNumber;
					String phone;
					Date day=new Date();
					String day11=new SimpleDateFormat("yyyy-MM-dd").format(day);
					if(textField.getText().equals("")||textField_1.getText().equals(""))
					{
						JOptionPane.showMessageDialog(null, "信息填写有误请重新填写");
					}else
					{
					bookNumber=textField.getText();
					phone=textField_1.getText();
					
					i = Integer.parseInt(bookNumber);
					
					if(book.getPhone(i).equals("-1"))
					{
						JOptionPane.showMessageDialog(null, "抱歉，没有您的预订！");
					}
					else
						if(book.getPhone(i).equals(phone)){
						
						if(book.getOrderDate(i).equals(day11)){
						String a=""+customer.searchWaiter();
						String table =""+waiter.searchEmptyTable(book.getNumOfPeo(i));
						JOptionPane.showMessageDialog(null, a+"号员工为您服务~\n请到"+table+"号桌就餐\n请点餐");
						
						book.deleteBook(i);
						
						int t=dinner.addDinnerNumber();
						ResMenuGUI menu=new ResMenuGUI(1,t);
						menu.setModal(true);
						menu.setLocationRelativeTo(null);
						menu.setVisible(true);
						}
						else{
							JOptionPane.showMessageDialog(null, "您的预订日期不是今天");
						}
					}
					else
						JOptionPane.showMessageDialog(null, " 抱歉，没有您的预订！");
						
					}
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		button.setBounds(215, 140, 93, 23);
		contentPane.add(button);
	}
}
